<template>
	<image src="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/top-bg.png" class="bg-color" mode="aspectFill"></image>
	<z-paging ref="paging" @scroll="pagScroll" refresher-only @onRefresh="onRefresh" :bg-color="scrollTop > 100 ? 'rgba(240, 240, 240,1)' : 'rgba(240, 240, 240,0)'">
		<!-- 之后-vue3 -->
		<template #top>
			<uv-navbar :bgColor="scrollTop > 100 ? '#fff' : 'rgba(0,0,0,0)'" autoBack :fixed="false" title="精选案例"></uv-navbar>
		</template>
		<block #refresher="{ refresherStatus }">
			<custom-refresher :status="refresherStatus" />
		</block>
		<!-- 内容 -->
		<view class="padidng-box ios">
			<uv-empty
				marginTop="100"
				v-if="!dataList.length"
				mode="list"
				icon="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png"
				:text="''"
				textColor="rgba(0,0,0,0)"
			></uv-empty>
			<view class="case-list-box">
				<view class="case-list-item" v-for="(item, index) in dataList" :key="index" @click="preFile(item)">
					<view class="icon-box" v-if="item.file_type == 2">
						<uv-icon name="play-right-fill" size="24" color="#ffffff"></uv-icon>
					</view>
					<image v-if="item.file_type == 1" :src="item.case_list[0].file_url" class="case-list-image" mode="aspectFill"></image>
					<video
						v-else
						:controls="false"
						:show-fullscreen-btn="false"
						:show-play-btn="false"
						:show-center-play-btn="false"
						:src="item.case_list[0].file_url"
						class="case-list-image"
						mode="aspectFill"
					></video>
					<view class="case-list-item-title ellipsis">{{ item.title }}</view>
				</view>
			</view>
		</view>
		<template #empty>
			<image class="no-data" src="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png" mode="widthFix"></image>
		</template>
	</z-paging>
	<!-- 视频预览 -->
	<preVideo :show="preVideoShow" :currVideo="currVideo" @close="maskClick"></preVideo>
</template>

<script setup>
import { ref, reactive, toRefs, toRef, getCurrentInstance, onMounted } from 'vue';
import { onShow, onLoad, onPageScroll } from '@dcloudio/uni-app';
const { proxy: that } = getCurrentInstance();

onLoad((e) => {
	wxHomepageData();
	uni.setNavigationBarColor({
		frontColor: data.scrollTop > 100 ? '#000000' : '#ffffff',
		backgroundColor: '#ffffff',
		animation: {
			duration: 400,
			timingFunc: 'easeIn'
		}
	});
});
const data = reactive({
	dataList: [],
	preVideoShow: false,
	currVideo: '',
	pagScroll(e) {
		data.scrollTop = e.detail.scrollTop;
		uni.setNavigationBarColor({
			frontColor: e.detail.scrollTop > 100 ? '#000000' : '#ffffff',
			backgroundColor: '#ffffff',
			animation: {
				duration: 400,
				timingFunc: 'easeIn'
			}
		});
	},
	scrollTop: 0
});
let { dataList, pagScroll, scrollTop, preVideoShow, currVideo } = toRefs(data);

const onRefresh = (page, num) => {
	wxHomepageData();
};
// 小程序配置数据
const wxHomepageData = () => {
	that.$Api
		.wxHomepageData()
		.then((res) => {
			console.log(res, '小程序配置数据');
			setTimeout(() => {
				// 1.5秒之后停止刷新动画
				that.$refs.paging.complete();
			}, 500);
			if (res.code == 200) {
				data.dataList = res.data.case; //res.data.case
			}
		})
		.catch((err) => {
			setTimeout(() => {
				// 1.5秒之后停止刷新动画
				that.$refs.paging.complete();
			}, 500);
		});
};
// 案例文件预览
let preFile = (item) => {
	console.log(item, '');
	if (item.file_type == 1) {
		// 图片
		let urls = [];
		item.case_list.forEach((file) => {
			urls.push(file.file_url);
		});
		uni.previewImage({
			urls
		});
	} else {
		data.preVideoShow = true;
		data.currVideo = item.case_list[0].file_url;
	}
};
// 关闭
let maskClick = () => {
	data.preVideoShow = false;
	data.currVideo = '';
};
</script>

<style lang="scss">
::v-deep .z-paging-content {
	transition: all 0.2s;
}

.bg-color {
	width: 100%;
	height: 708rpx;
	position: fixed;
	z-index: -1;
}

.padidng-box {
	width: 100%;
	padding: 20rpx 32rpx;
	box-sizing: border-box;

	.case-list-box {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		margin-bottom: 22rpx;

		.case-list-item {
			position: relative;
			width: 329rpx;
			margin-bottom: 26rpx;

			.icon-box {
				position: absolute;
				z-index: 1;
				top: 60rpx;
				left: 50%;
				transform: translate(-50%, 0);
				background-color: rgba(0, 0, 0, 0.6);
				width: 80rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 50%;
			}

			.case-list-image {
				width: 100%;
				height: 220rpx;
				border-radius: 16rpx;
			}

			&:nth-child(odd) {
				margin-right: 28rpx;
			}

			.case-list-item-title {
				font-weight: 500;
				font-size: 28rpx;
				color: #000000;
				margin-top: 12rpx;
				max-width: 100%;
			}
		}
	}
}
</style>
